//
//  XIGame.h
//  XI Engine
//
//  Created by Balazs Faludi on 25.05.09.
//  Copyright 2009 Balazs Faludi. All rights reserved.
//

#import <Foundation/Foundation.h>

@class EAGLView;
@class XIScene;
@class XIFont;

@protocol XIGameDelegate;
@protocol XIRenderView;

/** The XIGame class provides a centralized point of control and coordination for games created with XIEngine. 
 XIGame provides a shared instance that is allocated and initialized automatically for a new game project: [XIGame sharedGame].
 You create your game by subclassing XIScene and assigning an instance of it to the game by setting the activeScene property.
 The XIGame instance peridically sends an update and a draw message to it's active scene. You can change how often the active
 scene should receive these messages by changing the shared XIGame instance's targetFPS (frames per second) or targetSPF (seconds
 per frame) properties. XIScene subclasses are responsible to update and draw their content when receiving these messages. */
@interface XIGame : NSObject <UIAccelerometerDelegate>
{
	float	 targetFPS;		
	float    targetSPF;
	float    currentFPS;
	NSDate  *lastUpdate;
	XIScene *activeScene;
	BOOL	 paused;
	float	 multiplier;
	NSObject<XIGameDelegate> *delegate;
	NSObject<XIRenderView> *renderView;
	
// Private
	NSTimer  *_updateTimer;
	NSMutableArray *_fpsSample;
	NSDate *_fpsLastUpdate;
	XIFont *_fpsFont;
}

@property (nonatomic)		  float    targetFPS;
@property (nonatomic)		  float    targetSPF;
@property (nonatomic, readonly)float    currentFPS;
@property (nonatomic, readonly) NSDate  *lastUpdate;
@property (nonatomic, retain) XIScene *activeScene;
@property (nonatomic)		  BOOL	   paused;
@property (nonatomic, readonly)	float  multiplier;
@property (nonatomic, retain) NSObject<XIGameDelegate> *delegate;
@property (nonatomic, retain) NSObject<XIRenderView> *renderView;

+ (XIGame *)sharedGame;

- (void)start;
- (void)update;
- (void)draw;
- (void)startUpdating;
- (void)stopUpdating;

- (void)enableAccelerometer;
- (void)disableAccelerometer;

- (void)setupChipmunk;
- (void)setupAccelerometer;

/** @property targetFPS
 The frame per second rate the game should aim for.
 If you set this property, the game's targetSPF is updated accordingly.\ You can use both to set the game's refresh rate.
 \warning Note that this value is a target value only that can not reached always. If you have a computation intensive scene the actual 
 frame rate might drop below this value. To avoid speed changes of objects resulting in the changing refresh rate of your scene
 use the shared XIGame instance's multiplier value that is updated every frame. */

/** @property targetSPF
 The second per frame rate the game should aim for.
 If you set this property, the game's targerFPS is updates accordingly.\ You can use both to set the game's refresh rate.
 \see targetFPS */

/** @property activeScene
 The active scene of the game that is shown in fullscreen and is responible for showing the content.
 A game can have one active scene at a time. The game periodically sends an update and draw message to it's active scene.
 \see XIScene */

/** @property paused
 When set to NO, the game stops sending update and draw messages to it's active scene.
 This property can be used to pause the game when the phone goes to sleep mode for example.*/

/** @property multiplier
 This is a read-only property that can be used to avoid speed changes of the game resulting from chaning frame rates.
 The game's multiplier property is updated every frame, so in your XIObject and XIScene subclasses' update message implementation
 you can multiply any speed variables with it before actually changing an objects position. This will result in bigger position
 changes, if the current FPS is lower, leading to a constant speed of the object.*/


/** @fn start
 Starts the game.
 This method should only be called once at the beginning of the game. By default the shared game instance is sent a start message
 in the application's <code>applicationDidStart:</code> message. */

/** @fn enableAccelerometer
 Enables the accelerometer of the device for use in the game.
 If you enable the accelerometer, the game's active scene will receive an <code>accelerometer:didAccelerate:</code> message
 whenever the device was moved. Implement that message in your XIScene subclass to use accelerometer data in your game.*/

/** @fn disableAccelerometer
 Disables the accelerometer of the device.
 You can use this method to disable the accelerometer when you don't need it anymore. You can enable it again whenever you want.
 You can save battery life if you disable the accelerometer when you don't need it.*/
 
@end


@protocol XIGameDelegate
@optional
- (void)gameWillChangeActiveSceneTo:(XIScene *)scene;
- (void)gameDidChangeActiveSceneTo:(XIScene *)scene;
@end






